package com.panopset.compat;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

/**
 * Geometry Utilities.
 * 
 * @author Karl Dinwiddie
 * 
 */
public final class Geometry {

	/**
	 * 
	 * @param outerRectDimension
	 *            Outer Rectangle, centered at (0,0), Dimension.
	 * @param innerRectDimension
	 *            Inner Rectangle Dimension.
	 * @return Upper left corner of inner rectangle with outerRectDimension.
	 */
	public Point getCenterUpperLeftInZeroZeroRect(
			final Dimension outerRectDimension,
			final Dimension innerRectDimension) {
		return getCenterRectUpperLeftPoint(innerRectDimension, new Rectangle(0,
				0, outerRectDimension.width, outerRectDimension.height));
	}

	/**
	 * Get center start point of a line to be centered in a longer line.
	 * 
	 * @param centerLength
	 *            Center line length.
	 * @param outerStart
	 *            Outer line start.
	 * @param outerFinish
	 *            Outer line finish.
	 * @return Start point of the line to be centered in the outer line.
	 */
	public int getCenterStart(final int centerLength, final int outerStart,

	final int outerFinish) {
		return outerStart + ((outerFinish - outerStart - centerLength) / 2);
	}

	/**
	 * Get upper left point of a Rectangle with innerRectDimensions, which is
	 * centered in the outerRect.
	 * 
	 * @param innerRectDimensions
	 *            Inner Rectangle Dimensions.
	 * @param outerRect
	 *            Outer Rectangle.
	 * @return Upper left point of inner Rectangle.
	 */
	public Point getCenterRectUpperLeftPoint(
			final Dimension innerRectDimensions, final Rectangle outerRect) {
		return new Point(getCenterStart(innerRectDimensions.width, outerRect.x,
				outerRect.x + outerRect.width), getCenterStart(
				innerRectDimensions.height, outerRect.y, outerRect.y
						+ outerRect.height));
	}
	
    /**
     * Check to see if a point is in a rectangle.
     *
     * @param p
     *            Point to check.
     * @param r
     *            Rectangle to check.
     * @return True iff p is contained within r.
     */
    public boolean pointInRect(final Point p, final Rectangle r) {
        return pointInRect(p.x, p.y, r.x, r.y, r.width, r.height);
    }

    /**
     * Check to see if a point is in a rectangle.
     *
     * @param px
     *            Point x.
     * @param py
     *            Point y.
     * @param rx
     *            Rectangle x.
     * @param ry
     *            Rectangle y.
     * @param rw
     *            Rectangle width.
     * @param rh
     *            Rectangle height.
     * @return True iff p is contained within r.
     */
    public boolean pointInRect(final int px, final int py, final int rx,
            final int ry, final int rw, final int rh) {
        return pointInBounds(px, py, rx, ry, rx + rw, ry + rh);
    }

    /**
     * Check to see if a point is in a boundry.
     *
     * @param px
     *            Point x.
     * @param py
     *            Point y.
     * @param bx1
     *            Bounds point 1 x.
     * @param by1
     *            Bounds point 1 y.
     * @param bx2
     *            Bounds point 2 x.
     * @param by2
     *            Bounds point 2 y.
     * @return True iff p is contained within r.
     */
    public boolean pointInBounds(final int px, final int py,
            final int bx1, final int by1, final int bx2, final int by2) {
        if ((px < bx1 && px < bx2) || (px > bx1 && px > bx2)
                || (py < by1 && py < by2) || (py > by1 && py > by2)) {
            return false;
        }
        return true;
    }


}
